<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use think\Config;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Subsidy extends Permissions
{


    public function info()
    {
        $pid = input('id');

        $zhu = db('population')->where(['id'=>$pid])->find();

        $list = db('subsidy')->where(['uid'=>$pid])->select();


        $allMoney = db('subsidy')->where(['uid'=>$pid])->sum('sum');

        if (request()->isAjax()) {
            $post = input('post.');

            $res = db('subsidy')->insert($post);
            if ($res) {
                return 200;
            }else{
                return 202;
            }
            return $arr;
        }
        $this->assign('zhu',$zhu);
        $this->assign('allMoney',$allMoney);
        $this->assign('list',$list);
        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);
        return $this->fetch();
    }


    public function edit(){
        $input = input('get.');

        if (request()->isAjax()) {
            $input = input('post.');
            $id = $input['id'];
            unset($input['id']);
            $isok = db('subsidy')->where(['id' =>$id])->update($input);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
                $arr['msg'] = '操作失败,请刷新重试';
            }
            return $arr;
        }
        $data = db('subsidy')->where(['id' => $input['id']])->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function delete()
    {
        if (request()->isAjax()) {
            $id = input('id');

            $isok = db('subsidy')->where('id', $id)->delete();
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
                $arr['msg'] = "删除成功！";
            } else {
                $arr['code'] = 2;
                $arr['msg'] = "删除失败！";
            }
            return $arr;
        }
    }

    //回迁结算列表
    public function subsidy()
    {
        $list = db('population')->where(['isdel'=>1])->select();
        $nowTime = date('Y',time());
        $quTime = date("Y", strtotime("+1 year"));
        $str = $nowTime.",".$quTime;
        foreach ($list as $k => &$v) {
            //今年
            $v['nowSum']=db('subsidy')->where(['uid'=>$v['id'],'year'=>$nowTime])->sum('sum');
            $v['quSum']=db('subsidy')->where(['uid'=>$v['id'],'year'=>$quTime])->sum('sum');
            $map['uid'] = $v['id'];
            $map['year'] = ['not in',$str];
            $v['otherSum']=db('subsidy')->where($map)->sum('sum');

            $sum1 = bcadd($v['nowSum'],$v['quSum'],2);
            $v['sum'] = bcadd($sum1,$v['otherSum'],2);

        }
        $this->assign('list',$list);
        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);
        return $this->fetch();
    }
    //回迁结算录入页面
    public function subsidy_add()
    {
        $id = input('id');
        $this->assign('uid',$id);
        return $this->fetch(); 
    }

    public function subsidy_addnew()
    {
        if (request()->isAjax()) {
            $input = input('post.');
            // dump($input);die;
            $input['create_time'] = date('Y-m-d',time());
            $isok = db('subsidy')->insert($input);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
                $arr['msg'] = '操作失败,请刷新重试';
            }
            return $arr;
        }
    }

    public function subsidy_info()
    {
        // $id = input('id');
        // $info = db('subsidy')->where(['uid'=>$id])->find();
    }

}
